#!/usr/bin/env python
# coding=utf-8
"""
@Author  : youjia - 卞志伟
@file    : html_util.py
@contact : bianzhiwei@iyoujia.com
@time    : 2019-05-22 17:49 
@Desc    : 
@Software: PyCharm
"""
import os
import sys

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(pwd)
sys.path.append(father_path)
sys.path.append(grader_father)

import logging
from lxml import etree


def parse_html(html, xpath, node=None):
    """
    解析html
    :param html: html 文本
    :param xpath:  xpath
    :param node:xpath 解析出来的第几个节点
    :return:
    """
    try:
        if node is not None:
            return etree.HTML(html).xpath(xpath)[node]
        return etree.HTML(html).xpath(xpath)
    except Exception as e:
        logging.exception(e)
        if node is not None:
            return 0
        return 0
